package top.chengdongqing.mishop.user.favorite

import org.springframework.http.HttpStatus
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/users/favorites")
class FavoriteController(private val favoriteService: FavoriteService) {

    @GetMapping
    fun findFavorites(@AuthenticationPrincipal userId: Int): List<FavoriteProductDTO> {
        return favoriteService.findFavorites(userId)
    }

    @GetMapping("/{productId}")
    fun existsFavorite(@AuthenticationPrincipal userId: Int, @PathVariable productId: Int): Boolean {
        return favoriteService.existsFavorite(userId, productId)
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    fun addFavorite(
        @AuthenticationPrincipal userId: Int,
        @RequestBody favorite: FavoriteProductDTO,
    ) {
        favoriteService.addFavorite(userId, favorite)
    }

    @DeleteMapping("/{productId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun removeFavorite(@AuthenticationPrincipal userId: Int, @PathVariable productId: Int) {
        favoriteService.removeFavorite(userId, productId)
    }
}